home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4520 < prev    next >
Encoding:
Text File  |  1996-08-05  |  8.9 KB  |  186 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: void main() and other atrocities!
  5. Date: 04 Feb 1996 19:04:33 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb4120433@qcd.lanl.gov>
  8. References: <4eduaj$1aq@grouper.Exis.Net> <4epplj$egf@host-3.cyberhighway.net>
  9.     <4erjn2INN38b@keats.ugrad.cs.ubc.ca>
  10.     <9602021300.AA04359@dxmint.cern.ch>
  11.     <4f2rahINNmud@keats.ugrad.cs.ubc.ca>
  12. NNTP-Posting-Host: qcd.lanl.gov
  13. Mime-Version: 1.0
  14. Content-Type: text
  15. In-reply-to: c2a192@ugrad.cs.ubc.ca's message of 4 Feb 1996 09:44:49 -0800
  16.  
  17. In article <4f2rahINNmud@keats.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca
  18. (Kazimir Kylheku) writes: 
  19. <snip>
  20.    >    AxCrnA$ cc test.c
  21.    >
  22.    >    void main() {exit(0);}
  23.    >    .....^
  24.    >    %CC-E-NEEDNONVOID, In this statement, "main(...)" has void
  25. type, but occurs 
  26.    >    in a context that requires a non-void result.
  27.    >    at line number 2 in file DISK$L32:[DANPOP]TEST.C;3
  28.  
  29.    I know that it would break a compiler that generates an incompatible linkage
  30.    for functions returning int and void functions (assuming that the
  31. function you 
  32.    are calling does not return). But I just don't know of any that do (and it
  33.    would be silly to write one that generates subroutine linkage code
  34. which breaks 
  35.    when a void function is called that is assumed to be int).
  36.  
  37. You just saw above a compiler that does complain. In fact, more and
  38. more compiler technology is moving from `let the user do whatever
  39. (s)he wants' to `let us be helpful and give warnings for all
  40. questionable behaviour'. This may take away the `charm' of programming
  41. for old timers, who like the frontiersmen loved a reckless dangerous
  42. way of life; but, I do not think that anyone is their sane mind would
  43. deny it makes for better software tools. Now that we are a generation
  44. which has finally learned how to program the clock on the vcr,
  45. computers are slowly but surely advancing from machines that had to be
  46. treated with oh-so-much-awe to as much items of daily use as
  47. microwaves. Being smarter, we expect them to warn when we do something
  48. wrong ... this is the new wave. Nostalgia for the past is common, but
  49. that cannot hold back progress. In fact, it doesn't: like it or not,
  50. change comes; and one needs to adapt to survive.
  51.  
  52. With that prologue, I can think of one very important reason why the
  53. declaration of main must be int main and _not_ void main. The issue is
  54. type safe linkage. In fact, I always wondered why a computer was not
  55. smart enough to detect that a called entity cannot be the defined
  56. entity simply because they have different types! In a type safe scheme,
  57. the os will call an int main(int argc, char*argv[]), failing which it
  58. will call an int main(void) failing which it will abort! (one possible
  59. implementation: another would be that a library provides a
  60. non-overriding int main(int argc, char*argv[]) which calls an
  61. undefined int main(void);).
  62.  
  63. Sure, void main could have been included in that list. But, the point
  64. is that the language was specified with a `return code' in mind. Main
  65. is _supposed_ to return an integer valued code specifying whether
  66. there was an error. This is supposed to be the main means of
  67. specifying the return ... giving it a void type is, I believe,
  68. philosophically incorrect. The reason exit exists in the first place
  69. is because there is little facility of exception handling in C
  70. ... with proper exception handling mechanism, one probably wouldn't
  71. even need it.
  72.  
  73.    It's also atrocious that main() has to be treated as a special language
  74.    construct, and that a call to exit() in main() (when it is the last
  75. statement) 
  76.    is to be identical to a return.
  77.  
  78. What you are complaining about is not that main has to be treated
  79. specially: it is natural that it has to be. A program is initiated by
  80. the OS, and the OS needs to know how and what to initiate! If you
  81. declared main as `struct foo main(float x)', how _can_ the OS know
  82. what float it is expecting, and what to do with its struct return?
  83. Your objection is basically to the use of exit as the last statement
  84. to terminate main: and I agree this is a barbaric practice. exit
  85. should be used to exit from somewhere deep in the code, or to avoid
  86. complicated control structures. `goto's, multiple `return's, any
  87. `longjmp', `break's in loops and fall thrus in switch and `continue's
  88. as well as `exit' are all contrary to structured flow control: there
  89. use should be carefully monitored. 
  90.  
  91. A real language is a compromise between the different principles in
  92. what we currently know as good software engineering principles: a
  93. program is maintainable if it is simple, and it is maintainable if it
  94. is structured. As the two goals sometimes conflict, a succesful
  95. language specification can not take a purist stand. exit is necessary,
  96. and whether one likes it or not: it is.
  97.  
  98.    I don't advocate that people declare main() to return void (in fact
  99. I don't do 
  100.    so myself), but I don't see it as some sort of big mistake. In my
  101. own coding, I 
  102.    sometimes even define the argv/argc parameters even if I don't use
  103. them, just 
  104.    to meet the implicity prototype.
  105.  
  106. Actually, the standard specifically chose to allow int main(void), so
  107. you do not need to specify argc, argv. Logically, I believe this is a
  108. mistake: but standardizing a language which has already been in use by
  109. various people on differring architectures is a difficult task. A few
  110. compromises were made: that is unfortunate, but, I believe they are a
  111. historical necessity.
  112.  
  113.    I did read the relevant section in the FAQ. It is merely concerned with the
  114.    issue of eliminating compiler warnings stemming from calling exit()
  115. in main() 
  116.    despite a "void" declaration thereof. The issue that a void function may
  117.    not be compatible with a call to an int function doesn't seem that
  118. significant, 
  119.    since nobody in their right mind would design a compiler that way.
  120.  
  121. Yes, they would. There are reports that a Toshiba Satellite T2400CT
  122. with a 486/66 running  Windows 3.1.1 fails during link if one uses
  123. void main. I guess it is trying to do some kind of type safe
  124. linkage. As I am not sure of these reports (I belong to the old
  125. school where I trust myself and no one else :-), I cannot say more
  126. about this. I do however see reasons why a compiler writer will make
  127. the distinction.
  128.  
  129.    In any case, I program using ANSI C only about 30% of the time. The
  130.    old-fashioned function declarations are more elegant. The only truly useful
  131.    thing introduced in ANSI C is the standardization of
  132. variable-length arg list 
  133.    handling.  It's the only thing I really miss when working with non-ANSI
  134.    compilers. The other things I can handle myself: for instance, I
  135. don't need the 
  136.    compiler promoting arguments to their prototyped types for me. An
  137. explicit cast 
  138.    is more conscientious.
  139.  
  140. The main purpose of prototypes is not merely to do automatic
  141. promotions. The main point in their favour is that they allow the
  142. compiler to do more extensive consistency checks more easily. They
  143. take away the need for many casts which are potential sources of
  144. error. In other words they make for simpler expressions of an
  145. intuitive structure of algorithms which puts more burden on the
  146. compiler than on the code writer. This is what I consider the wave of
  147. the future.
  148.  
  149.    There is something magical about the old-style C that attracts me. I can't
  150.    quite put my finger on it. I was once a very good, careful coder. I
  151. wrote neat 
  152.    programs in Modula 2 and similar langauges, always had carefully designed
  153.    abastract data types. All my cardinal variables belonged to
  154. carefully declared 
  155.    and typed domains, arrays had typed ranges, etc.  It all went down hill from
  156.    there!
  157.  
  158.    Now I'm increasingly catching myself writing assembly code, as though I had
  159.    gone full-circle. The arguments of what a main() function ought to be sound
  160.    like the buzzing of flies to me.
  161.  
  162. As I said, things change. I believe what you are facing is what is
  163. called a `paradigm shift' in today's world of jargon. The job of a
  164. programmer today is not to micro manage the details, not to get that
  165. one extra cycle that can be had, but to write clear maintainable code
  166. whose beauty is not in its cleverness of register manipulation but in
  167. its abstract mathematical algorithmic structure.
  168.  
  169.    Can someone else identify with me? :)
  170.  
  171. I am sure many do. That, fortunately or otherwise, changes
  172. nothing. One way of thinking gives way to another, whether we like it
  173. or not. It is the exact same tautology of evolution: the structures
  174. and patterns of thought which are inherently most fit to survive, do
  175. survive best.
  176.  
  177. Cheers
  178. Tanmoy
  179. --
  180. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  181. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  182. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  183. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  184. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  185. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  186.